﻿using System;
using System.Configuration;
using System.Web;
using cz.uhul.DSMLv2.Server.Sql;


namespace cz.uhul.DSMLv2.Services
{



  public class LdServiceConfiguration : ConfigurationSection
  {





    /// <summary>
    /// Probes if configuration is valid. Call this function from Global.asax, onStart().
    /// <remarks>probes configuration of DSML service as deep as possible
    /// test if db connection exists, db version matches Assembly version,...</remarks>
    /// </summary
    public void Probe()
    {
      LdDatabase.RootDSE = this.RootDSE;
      LdDatabase.Probe();
    }


    //protected override void DeserializeSection(System.Xml.XmlReader reader)
    //{
    //  base.DeserializeSection(reader);
    //}





    [ConfigurationProperty("LdAdminOnlySecurityModel", IsRequired=false, DefaultValue= null)]
    public LdAdminOnlySecurityModel LdAdminOnlySecurityModel
    {
      get { return (LdAdminOnlySecurityModel)this["LdAdminOnlySecurityModel"]; }
      set 
      {
        if (this.LdFullSecurityModel.ElementInformation.IsPresent && value != null)
          throw new ConfigurationErrorsException("either AdminOnlySecurityModel or AdminOnlySecurityModel, but not both.");

        this["LdAdminOnlySecurityModel"] = value;
      }
    }





    [ConfigurationProperty("LdFullSecurityModel", IsRequired = false, DefaultValue = null)]
    public LdFullSecurityModel LdFullSecurityModel
    {
      get { return (LdFullSecurityModel)this["LdFullSecurityModel"]; }
      set 
      {
        if (this.LdAdminOnlySecurityModel.ElementInformation.IsPresent && value != null)
          throw new ConfigurationErrorsException("either AdminOnlySecurityModel or AdminOnlySecurityModel, but not both.");

        this["LdFullSecurityModel"] = value; 
      }
    }




    public LdSecurityModel SecurityModel
    {
      get 
      {
        return (LdFullSecurityModel.ElementInformation.IsPresent) ? (LdSecurityModel)LdFullSecurityModel : (LdSecurityModel)LdAdminOnlySecurityModel;
      }
    }





    /// <summary>
    /// Root directory service entry for current LD service
    /// </summary>
    [ConfigurationProperty("RootDSE", DefaultValue = "", IsRequired = true, IsKey = true)]
    public string RootDSE
    {
      get { return this["RootDSE"] as string; }
      set { this["RootDSE"] = value; }
    }




    //protected override void DeserializeElement(
    //       System.Xml.XmlReader reader,
    //        bool serializeCollectionKey)
    //{
    //  base.DeserializeElement(reader,
    //      serializeCollectionKey);
    //  // You can your custom processing code here.
    //}




  }

}